package com.anXin.user.controller;


import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

@Api(tags = "验证码接口" )
@Controller
public class KaptchaController {

    private static final Logger logger = LoggerFactory.getLogger(KaptchaController.class);
    public static HttpSession session;

    @ApiOperation("获取图片验证码")
    @RequestMapping(path = "/kaptcha",method = RequestMethod.GET)
    public void getKaptcha(HttpServletResponse response, HttpSession session) throws Exception {
        Random r=new Random();
        int a = r.nextInt(3) + 1;
        switch (a){
            case 1:get1(response, session);break;
            case 2:get2(response, session);break;
            case 3:get3(response, session);break;
//            case 4:get4(response, session);break;
//            case 5:get5(response, session);break;
        }
    }

    private void get1(HttpServletResponse response, HttpSession session){
        KaptchaController.session =session;
        SpecCaptcha captcha = new SpecCaptcha(130, 40);
        System.out.println("验证码为："+captcha.text());
        session.setAttribute("captcha",captcha.text());
        response.setContentType("image/png");
        try {
            OutputStream os = response.getOutputStream();
            captcha.out(os);
        } catch (IOException e) {
            logger.error("响应验证码失败" + e.getMessage());
        }
    }
    private void get2(HttpServletResponse response, HttpSession session) throws Exception {
        KaptchaController.session =session;
        GifCaptcha gifCaptcha = new GifCaptcha(130, 40);
        gifCaptcha.setCharType(Captcha.TYPE_ONLY_UPPER);
        //设置字体
        gifCaptcha.setFont(Captcha.FONT_5);
        System.out.println("验证码为："+gifCaptcha.text());
        session.setAttribute("captcha",gifCaptcha.text());
        response.setContentType("image/gif");
        OutputStream os = response.getOutputStream();
        gifCaptcha.out(os);
    }
//    private void get3(HttpServletResponse response, HttpSession session){
//        KaptchaController.session =session;
//        ChineseCaptcha chineseCaptcha = new ChineseCaptcha(130, 40);
//        System.out.println("验证码为："+chineseCaptcha.text());
//        session.setAttribute("captcha",chineseCaptcha.text());
//        response.setContentType("image/png");
//        try {
//            OutputStream os = response.getOutputStream();
//            chineseCaptcha.out(os);
//        } catch (IOException e) {
//            logger.error("响应验证码失败" + e.getMessage());
//        }
//    }
//    private void get4(HttpServletResponse response, HttpSession session){
//        KaptchaController.session =session;
//        ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(130, 40);
//        System.out.println("验证码为："+chineseGifCaptcha.text());
//        session.setAttribute("captcha",chineseGifCaptcha.text());
//        response.setContentType("image/gif");
//        try {
//            OutputStream os = response.getOutputStream();
//            chineseGifCaptcha.out(os);
//        } catch (IOException e) {
//            logger.error("响应验证码失败" + e.getMessage());
//        }
//    }
    private void get3(HttpServletResponse response, HttpSession session){
        KaptchaController.session =session;
        ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(180, 40);
        //设置几位数的运算，几个数计算
        arithmeticCaptcha.setLen(3);
        //打印算式
        System.out.println("算式为："+arithmeticCaptcha.getArithmeticString());
        System.out.println("验证码为："+arithmeticCaptcha.text());
        session.setAttribute("captcha",arithmeticCaptcha.text());
        response.setContentType("image/png");
        try {
            OutputStream os = response.getOutputStream();
            arithmeticCaptcha.out(os);
        } catch (IOException e) {
            logger.error("响应验证码失败" + e.getMessage());
        }
    }
}
